const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}

const CLIPDATA = CLIPDATA_TYPE.mid

const CONFIG = {
    chance: 4,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        fixedHp: 4780,
        fixedAtt:311,
        hpRate: 46.6 * 3 ,
        // reaction: 187,
        // dmgRate: 46.6,
        // criticalStrike: 62.2,
    },
    clips_initial: [
        ["charging", "hpRate", "criticalRate", "criticalStrike"],
        ["charging", "hpRate", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["charging", "reaction", "criticalRate", "criticalStrike"],
        ["reaction", "criticalStrike", "charging", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, hpRate: 20}, // 2+2
        { hpRate: 20 } // 2命
    ]
}

let charaters = {
    units : {
        name: "坎蒂丝", 
        dmgZhi: 0,
        charging: 100,
        dmgRate: 0,
        defRate: 0,
        defence: 682.52,
        weapontype: "qiang",
        hpRate:24,
        attRate: 0,
        hp:10875,
        base: 212.4,    
        criticalRate: 5,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

/*
    坎蒂丝 90级面板 血10875 白字212.4 双爆5/50 血量加成:24%
    lv10 普攻 1.2529 + 1.2512 + 0.7222+0.8826 + 1.972  重击2.4548 
    e 盾牌冲击 长按住:顶盾生成护盾蓄力 完成蓄力一发高伤害 护盾持续到e结束
               lv10: 点按 0.2212%hp  长按 0.3677%hp 护盾0.216%hp
               lv13: 点按 0.2641%hp  长按 0.4341%hp 护盾0.255%hp
    q 开大造成一次伤害 为场上角色 普攻增伤20% 近战附着水元素 持续9s 切人触发水冲击波只能触发3次水冲击波
             lv10 伤害 0.119%hp   lv13: 0.1405%hp

    天赋 长按e时受到攻击会立刻蓄力完成
    天赋 q加持下的角色获得普攻增伤 普攻增伤为 坎蒂丝每1000点生命值上限提高0.5%。

    1命 q持续时间+3s
    2命 e打中敌人自己+20%血
    3命 q+3
    4命 缩短e长安的冷却时间 缩短到6s 原来9s
    5命 e+3
    6命 q期间场上角色普攻触发额外冲击 倍率0.2093%hp 2.3触发一次 
*/ 


let tmp = {}
CONFIG.clips_initial.forEach(item => {
    item.forEach(item2 => {
        if(tmp[item2]) {
            tmp[item2] += 1
        } else {
            tmp[item2] = 1
        }
    })
})

let cid = "units"
let count_kit = 0;
let combination = getCombination(Array(5*CONFIG.chance + 1).fill().map((_,i)=>i), CONFIG.chance, Object.entries(tmp))
let clips_initial_data = calc_clips_initial()
let fs = require("fs")
calc(combination);
function calc(clipSecondData) {
    let charactersDmg = [];
    clipSecondData = clipSecondData.map(clipSecondDataItem => {
        let clipSecond = {}
        clipSecond.des = clipSecondDataItem;
        clipSecond.data = {} 
        for (let secondKey in clipSecond.des) {
            let count =  clipSecond.des[secondKey]
            clipSecond.data[secondKey] = count * CLIPDATA[secondKey]
        }
        return clipSecond
    })
    
    Object.values(WEAPONS).forEach(weapon => {
        if(charaters[cid].weapontype && weapon.weapontype != charaters[cid].weapontype && weapon.weapontype!="all") {return}
        charactersDmg.push(getDMG([charaters[cid],weapon,clips_initial_data,{...CONFIG.clips_main}], clipSecondData, (data,second) => {
            count_kit++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100) + data.fixedHp;
            
            if(weapon.skill) { weapon.skill(data); }
          
            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }
      
        
            let eDmg = getDmg({ data:{ // 
                ...data, dmgZhi: data.dmgZhi + data.hp*0.3677
            }, rate:0}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let qDmg = getDmg({ data:{ // 
                ...data, dmgZhi: data.dmgZhi + data.hp*0.1405,
            }, rate:0}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);
            let q6minDmg = getDmg({ data:{ // 
                ...data, dmgZhi: data.dmgZhi + data.hp*0.2093,
            }, rate:0}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);

            let pugongdmgAdd = 20 + Math.floor(data.hp/1000)*0.5 

            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }

            let point = pugongdmgAdd; //  eDmg * 2 + qDmg * 3 + q6minDmg * 5
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            return {point, data, second, attack, dmg:{eDmg,qDmg,q6minDmg,pugongdmgAdd}}
        }))
    })
    
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    let sortf = JSON.parse(fs.readFileSync("./sort.js").toString()) 
    let choose = charactersDmg[0];
    console.log(choose);

    let name = choose.data.name
    let keys = "_"+choose.data.weaponname+"_6命无拐2+2毕业"
    let sortfData = {key: name+keys, point:choose.point, dmg:JSON.parse(JSON.stringify(choose.dmg))}
    if(sortfData.point && sortfData.point > 0) {
        let org = sortf.findIndex(item3 => item3.key == sortfData.key)
        if(org >= 0){
            sortf[org] = sortfData
        } else {
            sortf.push(sortfData)
        }
        sortf.sort((b,a) => a.point-b.point)
        // fs.writeFileSync("./sort.js", JSON.stringify(sortf))
    }
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count_kit);
}

function getDMG(dataarr, clipSecond, getRet){
    clipSecond = clipSecond.map(second => {
        let panel = objValueAdd([...dataarr, second.data]);
        return  getRet(panel, second)
    })
    clipSecond.sort((b,a)=> a.point-b.point)
    return clipSecond[0]
}

function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
function getCombination (arr, chance, entrys) {
    let num = 5
    var r = [];
    (function f(t, a, n) {
        if (n === 0) {
            if(t.reduce((a1,b1) => a1+b1) !== chance * 5){return}
            let ret = {}
            for (let i in t) {
                if(t[i] > entrys[i][1]*chance){return}
                ret[entrys[i][0]] = t[i]
            }
            r.push(ret)
            return ;
        }
        for (var i = 0, l = a.length; i < l; i++) {
            f(t.concat(a[i]), a, n - 1);
        }
    })([], arr, num);
    return r;
}